package com.hunan.api.catv.service.impl.iptv;

import com.google.gson.JsonObject;
import com.hunan.api.catv.utils.GsonUtils;
import com.hunan.api.catv.utils.Hmacsha256Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

import java.net.URLEncoder;
import java.util.Map;

@Service
public class IptvService {

    public static Logger log = LoggerFactory.getLogger(IptvService.class);

    @Autowired
    RestTemplate restTemplate;

    public JsonObject queryTelecastList(String domain, String secret, Map<String, String> body) {
        StringBuilder url = new StringBuilder(domain);
        url.append("/IPTV_CMS/syncInterface/queryTelecastList");
        String sign = Hmacsha256Utils.formatUrlMap(body, secret);
        body.put("sign", sign);
        HttpEntity<String> requestEntity = new HttpEntity<String>(GsonUtils.toJson(body));
        ResponseEntity<String> result = restTemplate.exchange(url.toString(), HttpMethod.POST, requestEntity, String.class, "");
        if (result.getStatusCodeValue() == HttpStatus.OK.value()) {
            return GsonUtils.fromJson(result.getBody(), JsonObject.class);
        }
        return null;
    }

    public JsonObject queryScheduleList(String domain, String secret, Map<String, String> body) {
        StringBuilder url = new StringBuilder(domain);
        url.append("/IPTV_CMS/syncInterface/queryScheduleList");
        String sign = Hmacsha256Utils.formatUrlMap(body, secret);
        body.put("sign", sign);
        HttpEntity<String> requestEntity = new HttpEntity<String>(GsonUtils.toJson(body));
        log.info("body:{}", body);
        ResponseEntity<String> result = restTemplate.exchange(url.toString(), HttpMethod.POST, requestEntity, String.class, "");
        if (result.getStatusCodeValue() == HttpStatus.OK.value()) {
            return GsonUtils.fromJson(result.getBody(), JsonObject.class);
        }
        return null;
    }

    public JsonObject getPlayAddressByIdWNApp(String domain, String signStr, String secretKey, Map<String, Object> uriVariables) {
        StringBuilder url = new StringBuilder(domain);
        url.append("/IPTV_EPG/MediaAssets/GetPlayAddressByIdWNApp");
        url.append("?version=1.0");
        url.append("&userId={userId}");
        url.append("&userMac={userMac}");
        url.append("&userIp={userIp}");
        url.append("&authTimestamp={authTimestamp}");
        url.append("&playAuthSign={playAuthSign}");
        url.append("&platform={platform}");
        url.append("&videoId={videoId}");
        url.append("&videoType={videoType}");
        if (!ObjectUtils.isEmpty(uriVariables.get("beginTime"))) {
            url.append("&beginTime={beginTime}");
        }
        if (!ObjectUtils.isEmpty(uriVariables.get("endTime"))) {
            url.append("&endTime={endTime}");
        }
        try {
            String playAuthSign = URLEncoder.encode(Hmacsha256Utils.hmacsha256(signStr, secretKey), "utf-8");
            uriVariables.put("playAuthSign", playAuthSign);
            HttpEntity<String> requestEntity = new HttpEntity<String>("");
            ResponseEntity<String> result = restTemplate.exchange(url.toString(), HttpMethod.GET, requestEntity, String.class, uriVariables);
            if (result.getStatusCodeValue() == HttpStatus.OK.value()) {
                return GsonUtils.fromJson(result.getBody(), JsonObject.class);
            }
        } catch (Exception e) {
            log.error("getPlayAddress Error:{}", e);
        }
        return null;
    }
}
